# lib/stair_master/concerns/getters.rb
##
##
##
module StairMaster
  module Concerns
    module Getters
      extend ActiveSupport::Concern

      private # -------------------------------------------

      def get_step_index_by_name(step_name)
        available_steps.find_index step_name
      end

      def get_step_name_by_index(step_index)
        available_steps[step_index]
      end

      def get_step_by_index(step_index)
        get_step_by_name available_steps[step_index]
      end

      def get_step_by_name(step_name)
        workflow_map.workflow_steps[step_name]
      end

      ## NEXT STEP ----------------------------------------
      def get_next_step_index
        on_last_step? ? nil : find_next_step_index
      end

      def find_next_step_index(step_index=nil)
        next_index  = step_index.nil? ? current_step_index.next : step_index.next
        step        = get_step_by_index next_index

        if step.nil?
          nil
        elsif step.skip?(self)
          if is_last_step?(step.key)
            return nil
          else
            find_next_step_index(next_index)
          end
        else
          next_index
        end
      end

      ## GET PREVIOIUS STEP INDEX -------------------------
      def get_previous_step_index(step_index=nil)
        on_first_step? ? nil : find_previous_step_index
      end

      def find_previous_step_index(step_index=nil)
        previous_index  = step_index.nil? ? current_step_index.pred : step_index.pred
        step            = get_step_by_index previous_index

        if step.nil?
          nil
        elsif step.skip?(self)
          if is_first_step?(step.key)
            return nil
          else
            find_previous_step_index(previous_index)
          end
        else
          previous_index
        end
      end
    end
  end
end
